/**
 * @class common.colorPicker
 * @extends common.base.object
 * 省市县三级联动
 <pre>
new Address({wrap: $('.area')});
 </pre>
 */
define(function (require) {
    var Base = require('common/base/object'),
        Tpl = require('text!./templates/address.html');
    return Base.extend({
        /**
         * 初始化函数
         * @param  {Object} params 
         */
        initialize: function (params) {
            var _this = this;
            Base.prototype.initialize(params);
            _this.params = params;
            $.getScript(_global.static_url + 'vendor/area.js', function () {
                _this.data = areaData;
                _this.provinceList = _this.getProvinceList();
                _this.provinceSelected = _this.params.wrap.data('province');
                _this.cityList = _this.getCityList();
                _this.citySelected = _this.params.wrap.data('city');
                _this.regionList = _this.getRegionList();
                _this.regionSelected = _this.params.wrap.data('region');
                _this._render();
            });
        },
        getProvinceList: function () {
            var _this = this,
                result = [];
            _.each(_this.data, function (val, key) {
                result.push(key);
            });
            return result;
        },
        getCityList: function () {
            var _this = this;
            if (_.empty(_this.provinceSelected)) {
                return false;
            }

            var result = [],
                data = _this.data[_this.provinceSelected].items;
            _.each(data, function (val, key) {
                result.push(key);
            });
            return result;
        },
        getRegionList: function () {
            var _this = this;
            if (_.empty(_this.provinceSelected) || _.empty(_this.citySelected)) {
                return false;
            }
            var result = [],
                data = _this.data[_this.provinceSelected].items[_this.citySelected].items;
            _.each(data, function (val, key) {
                result.push(key);
            });
            return result;
        },

        _render: function () {
            var _this = this,
                html = _this.render(Tpl, {
                    provinceList: _this.provinceList,
                    provinceSelected: _this.provinceSelected,
                    cityList: _this.cityList,
                    citySelected: _this.citySelected,
                    regionList: _this.regionList,
                    regionSelected: _this.regionSelected
                });
            _this.params.wrap.html(html);
            _this.bindEvents();
        },
        bindEvents: function () {
            var _this = this;
            _this.$('select').change(function () {
                var $this = $(this),
                    val = $this.val();
                if ($this.hasClass('province')) {
                    _this.provinceSelected = val;
                    _this.cityList = _this.getCityList();
                    _this.regionList = [];
                } else if ($this.hasClass('city')) {
                    _this.citySelected = val;
                    _this.regionList = _this.getRegionList();
                } else if ($this.hasClass('region')) {
                    _this.regionSelected = val;
                }
                _this._render();
            });
        }
    });
});
